package cn.xdf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lscl
 * @version 1.0
 * @intro: @CookieValue
 */
@Controller
@RequestMapping("/demo03")
public class Demo03Controller_CookieValue {

    @GetMapping("/demo01")
    public void demo01(@CookieValue String JSESSIONID, HttpServletResponse response) throws IOException {

        response.setContentType("text/html;charset=utf8");
        response.getWriter().write("JSESSIONID: " + JSESSIONID + "<hr />");
    }


    @GetMapping("/demo02")
    public void demo02(HttpServletResponse response) throws IOException {

        Cookie cookie = new Cookie("cityName", "guangzhou");//设置一个cookie

        response.addCookie(cookie);
        response.setContentType("text/html;charset=utf8");
        response.getWriter().write("ok");
    }

    @GetMapping("/demo03")
    public void demo03(@CookieValue String cityName,HttpServletResponse response)throws IOException{

        response.setContentType("text/html;charset=utf8");
        response.getWriter().write("cityName: "+cityName+"<hr/>" );
        response.getWriter().write("ok");
    }
}
